package com.leetcode.partition9;

/**
 * @author `RKC`
 * @date 2021/12/13 11:21
 */
public class LC807保持城市天际线 {

    public static int maxIncreaseKeepingSkyline(int[][] grid) {
        int n = grid.length, answer = 0;
        //分别保存从北往南看和从东往西看能看到的最高高度
        int[] southToNorth = new int[n], westToEast = new int[n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                southToNorth[i] = Math.max(southToNorth[i], grid[j][i]);
                westToEast[i] = Math.max(westToEast[i], grid[i][j]);
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                answer += Math.min(southToNorth[j], westToEast[i]) - grid[i][j];
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        int[][] grid = {{3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3}, {0, 3, 1, 0}};
        System.out.println(maxIncreaseKeepingSkyline(grid));
    }
}
